package com.chrray.position.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.chrray.position.domain.dto.*;
import com.chrray.position.domain.dto.search.*;
import com.chrray.position.domain.vo.FdJobPostingsVO;
import com.chrray.position.service.position.FdJobService;
import com.chrray.position.service.position.FdJobSubcategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liujianxin
 * @date 2024/08/28 22:11
 */
@RestController
@RequestMapping("/api/app/jobs")
@Api(tags = "职位相关")
@AllArgsConstructor
public class FdJobsController {
    private final FdJobService jobService;
    private final FdJobSubcategoryService fdJobSubcategoryService;

    @GetMapping("/district-selection")
    @ApiOperation("省/市/区/街道下拉选")
    public R<List<DictionaryDTO>> districtSelection(@RequestParam("code") long code) {
        return R.ok(fdJobSubcategoryService.districtSelection(code));
    }


    @GetMapping("/selections")
    @ApiOperation("职位选择列表,级联/二级")
    public R<List<DictionaryDTO>> selections() {
        return R.ok(fdJobSubcategoryService.selections());
    }

    @PostMapping("/publish")
    @ApiOperation("发布职位")
    public R<String> publishJob(@RequestBody @Validated FdJobPostingsDTO job){
        jobService.publishJob(job);
        return R.ok("发布成功");
    }



    // 雇工,首页抢单,前端定时轮询,查询附近100km的订单
    @PostMapping("/nearby-jobs")
    @ApiOperation("附近的订单")
    public R<IPage<GrabJobsDTO>> nearbyJobs(@RequestBody @Validated GrabJobSearch search){
        return R.ok(jobService.nearbyJobs(search));
    }


    // 职位列表
    @PostMapping("/list")
    @ApiOperation("职位列表")
    public R<IPage<GrabJobsDTO>> list(@RequestBody @Validated  JobPageSearch search){
        return R.ok(jobService.pageList(search));
    }

    // 抢单
    @PostMapping("/grab-jobs")
    @ApiOperation("抢单")
    public R<Boolean> grabJob(@RequestBody @Validated GrabJob body) {
        return R.ok(jobService.grabJob(body.getJobId()));
    }

    // 职位详情
    @GetMapping("/detail")
    @ApiOperation("职位详情")
    public R<FdJobPostingsVO> jobDetail(@Param("jobId") Long jobId){
        return R.ok(jobService.jobDetail(jobId));
    }

    // 推荐的职位列表, 10个?
    @PostMapping("/recommend")
    @ApiOperation(value = "推荐的职位列表", notes = "用在职位详情下面的推荐.传职位类型和经纬度")
    public R<List<GrabJobsDTO>> recommend(@RequestBody JobPageSearch search){
        return R.ok(jobService.recommend(search));
    }


    // 我的报名/我的零工
    @PostMapping("/worker/my-apply")
    @ApiOperation("雇工: 我的报名/我的零工")
    public R<IPage<GrabJobsDTO>> myApply(@RequestBody @Validated MyJobSearchSearch search){
        return R.ok(jobService.myApply(search));
    }

    // 雇工-职位取消
    @GetMapping("/worker/cancel-apply")
    @ApiOperation("雇工: 取消申请")
    public R<Boolean> cancelApply(@RequestParam Long jobId){
        return R.ok(jobService.cancelApply(jobId));
    }

    // 雇工-收藏
    @GetMapping("/worker/favorites")
    @ApiOperation("雇工: 收藏某个职位")
    public R<Boolean> collectJob(@RequestParam Long jobId){
        return R.ok(jobService.collectJob(jobId));
    }
    // 雇工-取消收藏
    @GetMapping("/worker/cancel-favorites")
    @ApiOperation("雇工: 取消收藏某个职位")
    public R<Boolean> cancelCollectJob(@RequestParam Long jobId){
        return R.ok(jobService.cancelCollectJob(jobId));
    }

    // 雇工-我的收藏列表
    @PostMapping("/worker/favorites-list")
    @ApiOperation("雇工: 我的收藏列表")
    public R<IPage<GrabJobsDTO>> favoritesList(@RequestBody @Validated MyJobSearchSearch search){
        return R.ok(jobService.favoritesList(search));
    }

    // 雇主-我的发布: 职位状态:0-待审核／-1 驳回／5-匹配中/1-进行中／2-已完成／3-已结束/4-已下架;
    @PostMapping("/boss/my-publish")
    @ApiOperation("雇主: 我的发布")
    public R<IPage<MyPubJobsDTO>> myPublish(@RequestBody BossJobPubSearch search){
        return R.ok(jobService.bossMyPublish(search));
    }

    // 发布的职位详情: 待录用/已录用/已完工
    @PostMapping("/boss/job-detail")
    @ApiOperation("雇主: 发布的职位详情(查看待录用/已录用/待完工雇工列表)")
    public R<IPage<JobRelWorkerDTO>> jobRelWorker(@RequestBody @Validated  BossJobRelWorkerSearch search){
        return R.ok(jobService.jobRelWorker(search));
    }


    // 雇主-录用雇工
    @GetMapping("/boss/employ")
    @ApiOperation("雇主: 录用雇工")
    public R<Boolean> employ(@RequestParam Long jobId, @RequestParam Long userId){
        return R.ok(jobService.employ(jobId,userId));
    }

    // 雇主-对指定雇工完工
    @GetMapping("/boss/complete")
    @ApiOperation("雇主: 对指定雇工完工")
    public R<Boolean> complete(@RequestParam Long jobId, @RequestParam Long userId){
        return R.ok(jobService.complete(jobId,userId));
    }

    // 雇主-附近的零工
    @PostMapping("/boss/nearby-workers")
    @ApiOperation("雇主: 附近的零工")
    public R<IPage<NearbyWorkerDTO>> nearbyWorkers(@RequestBody @Validated GrabJobSearch search){
        return R.ok(jobService.nearbyWorkers(search));
    }

    // 雇主-将职位完工
    @GetMapping("/boss/completed-job")
    @ApiOperation("雇主: 将职位状态置为完工")
    public R<Boolean> completeJob(@RequestParam Long jobId){
        return R.ok(jobService.completeJob(jobId));
    }


    // 雇主-对职位重新发布
    @PostMapping("/boss/re-publish")
    @ApiOperation("雇主: 对职位重新发布")
    public R<Boolean> rePublish(@RequestBody @Validated FdJobPostingsDTO job){
        return R.ok(jobService.rePublish(job));
    }

    // 雇主-查看雇工详情(复用简历接口)


    // 雇工-分享职位
    @GetMapping("/share-job")
    @ApiOperation("雇工: 分享职位,获取token,追加在url再分享出去")
    public R<String> shareJob(@RequestParam Long jobId){
        return R.ok(jobService.shareJob(jobId));
    }

    @GetMapping("/end-job")
    @ApiOperation("结束")
    public R<String> endJob(@RequestParam Long jobId){
        return jobService.endJob(jobId);
    }

    @GetMapping("/cancel-job")
    @ApiOperation("取消录用")
    public R<String> cancelJob(@RequestParam("jobId") Long jobId,@RequestParam("userId") Long userId){
        return jobService.cancelJob(jobId,userId);
    }
}
